package cn.tx.config.zookeeper;


import lombok.extern.slf4j.Slf4j;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @author: 可乐加糖
 * @create: 2023-02-28 11:16
 * @Description: zk初始化配置
 */
@Slf4j
@Configuration
public class ZookeeperConfig {
    @Autowired
    private ZookeeperProperties zookeeperProperties ;

    private static CuratorFramework client = null ;
    /**
     * 初始化
     */
    @PostConstruct
    public void init (){
//        //重试策略，初试时间1秒，重试10次
//        RetryPolicy policy = new ExponentialBackoffRetry(
//                zookeeperProperties.getBaseSleepTimeMs(),
//                zookeeperProperties.getMaxRetries());
//        //通过工厂创建Curator
//        client = CuratorFrameworkFactory.builder()
//                .connectString(zookeeperProperties.getServer())
//                .authorization("digest",zookeeperProperties.getDigest().getBytes())
//                .connectionTimeoutMs(zookeeperProperties.getConnectionTimeoutMs())
//                .sessionTimeoutMs(zookeeperProperties.getSessionTimeoutMs())
//                .retryPolicy(policy).build();
//        //开启连接
//        client.start();
//        log.info("zookeeper 初始化完成...");
    }
    public static CuratorFramework getClient (){
        return client ;
    }
    public static void closeClient (){
        if (client != null){
            client.close();
        }
    }
}
